package com.ruoyi.web.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.web.vo.MiningRecordVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.MiningRecord;
import com.ruoyi.web.service.IMiningRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 挖矿记录Controller
 * 
 * @author ruoyi
 * @date 2022-12-30
 */
@RestController
@RequestMapping("/mining")
public class MiningRecordController extends BaseController
{
    @Autowired
    private IMiningRecordService miningRecordService;

    /**
     * 查询挖矿记录列表
     */
    @Anonymous
    @GetMapping("/list")
    public R<List<MiningRecordVO>> list()
    {
        List<MiningRecord> list = miningRecordService.selectMiningRecordList(new MiningRecord());
        List<MiningRecordVO> voList = new ArrayList<>();
        for (MiningRecord record : list) {
            MiningRecordVO vo = new MiningRecordVO();
            String address = record.getAddress();
            address = address.substring(0, 6) + "**********" + address.substring(35);
            vo.setAddress(address);
            vo.setNum(record.getNum().setScale(2, RoundingMode.DOWN));
            voList.add(vo);
        }
        return R.ok(voList);
    }

    /**
     * 导出挖矿记录列表
     */
    @PreAuthorize("@ss.hasPermi('web:record:export')")
    @Log(title = "挖矿记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiningRecord miningRecord)
    {
        List<MiningRecord> list = miningRecordService.selectMiningRecordList(miningRecord);
        ExcelUtil<MiningRecord> util = new ExcelUtil<MiningRecord>(MiningRecord.class);
        util.exportExcel(response, list, "挖矿记录数据");
    }

    /**
     * 获取挖矿记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(miningRecordService.selectMiningRecordById(id));
    }

    /**
     * 新增挖矿记录
     */
    @PreAuthorize("@ss.hasPermi('web:record:add')")
    @Log(title = "挖矿记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MiningRecord miningRecord)
    {
        return toAjax(miningRecordService.insertMiningRecord(miningRecord));
    }

    /**
     * 修改挖矿记录
     */
    @PreAuthorize("@ss.hasPermi('web:record:edit')")
    @Log(title = "挖矿记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MiningRecord miningRecord)
    {
        return toAjax(miningRecordService.updateMiningRecord(miningRecord));
    }

    /**
     * 删除挖矿记录
     */
    @PreAuthorize("@ss.hasPermi('web:record:remove')")
    @Log(title = "挖矿记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(miningRecordService.deleteMiningRecordByIds(ids));
    }
}
